#include "string.h"
namespace wanghao
{
    const size_t string::npos = -1;
    //迭代器
    using iterator = char*;
    using const_iterator = const char*;
    //无参构造
    // string::string()
    //     :_str(new char[1]{'\0'})
    //     ,_size(0)
    //     ,_capacity(0)
    // {}
    //带参构造
    string::string(const char* str)
        :_size(strlen(str))
    {
        //开空间
        _str = new char[_size + 1];
        //拷贝数据
        for(int i = 0;i < _size;i++)
        {
            *(_str + i) = *(str + i);
        }
        _capacity = _size;
    }
    //拷贝构造
    string::string(const string& s)
    {
        //开空间
        _capacity = s._capacity;
        _str = new char[_capacity + 1];
        //更新_size
        _size = s._size;
        //拷贝数据
        for(size_t i = 0;i < _size;i++)
        {
            *(_str + i) = *(s._str + i);
        }
    }
    //赋值构造
    string& string::operator=(string s)
    {
        swap(s);
        return *this;
    }
    //pos位置开始的len个构造
    string::string(const string& s,size_t pos,size_t len)
    {
        assert(pos < s._size);
        if(len >= _size - pos)
        {
            len = s._size - pos;
        }
        _str = new char[len + 1];
        _size = _capacity = len;
        //拷贝数据
        size_t index = 0;
        for(size_t i = pos;i < pos + len;i++)
        {
            *(_str + index++) = *(s._str + i);
        }
    }
    //计算字符串长度
    size_t string::strlen(const char* str)
    {
        size_t count = 0;
        while(*str != '\0')
        {
            count++;
            ++str;
        }
        return count;
    }
    //析构函数
    string::~string()
    {
        if(_str != nullptr)
        {
            delete[] _str;
            _capacity = _size = 0;
        }
    }
    //迭代器
    iterator string::begin()
    {
        return _str;
    }
    iterator string::end()
    {
        return _str + _size;
    }
    //const迭代器
    const_iterator string::begin() const
    {
        return _str;
    }
    const_iterator string::end() const
    {
        return _str + _size;
    }
    //size
    size_t string::size() const
    {
        return _size;
    }
    //empty
    bool string::empty() const
    {
        return !_size;
    }
    //清空串
    void string::clean()
    {
        *_str = '\0';
        _size = 0;
    }
    //空间容量
    size_t string::capacity() const
    {
        return _capacity;
    }
    //c_str
    iterator string::c_str() const
    {
        return _str;
    }
    //开空间
    void string::reserve(size_t n)
    {
        //判断n是否合理
        if(n > _capacity)
        {
            //申请新空间，+1的是给'\0'预留的空间
            char* tmp = new char[n + 1];
            //拷贝数据
            for(size_t i = 0;i < size();i++)
            {
                *(tmp + i) = *(_str + i);
            }
            //释放旧空间
            delete[] _str;
            //让_str指向新空间
            _str = tmp;
            //更新_capacity
            _capacity = n;
        }
    }
    //尾插
    void string::push_back(char ch)
    {
        //判断空间是否足够
        // if(_size == _capacity)
        // {
        //     reserve(_capacity == 0 ? 4 : 2 * _capacity);
        // }
        // *(_str + _size + 1) = *(_str + _size);
        // *(_str + _size) = ch;
        // _size++;
        insert(_size,ch);
    }
    //尾插一段字符串
    void string::append(const char* str)
    {
        // size_t len = strlen(str);
        // if(_size + len > _capacity)
        // {
        //     //扩容
        //     size_t newCapacity = 2 * _capacity;
        //     if(_size + len > newCapacity)
        //     {
        //         newCapacity = _size + len;
        //     }
        //     reserve(newCapacity);
        // }
        // for(size_t i = 0;i < len;i++)
        // {
        //     *(_str + _size++) = *(str + i);
        // }
        insert(_size,str);
    }
    //尾删
    void string::pop_back()
    {
        // assert(!empty());
        // *(_str + _size - 1) = '\0';
        // _size--;
        erase(_size - 1);
    }
    //指定位置插入字符
    void string::insert(size_t pos,char ch)
    {
        //检查pos位置是否合理
        assert(pos <= _size);
        //判断空间是否足够
        if(_size == _capacity)
        {
            reserve(_capacity == 0 ? 4 : 2 * _capacity);
        }
        //挪动数据
        size_t i = _size + 1;
        while(i > pos)
        {
            *(_str + i) = *(_str + i - 1);
            i--;
        }
        *(_str + pos) = ch;
        _size++;
    }
    //指定位置插入字符串
    void string::insert(size_t pos,const char* str)
    {
        assert(pos <= _size);
        size_t len = strlen(str);
        if(_size + len > _capacity)
        {
            //扩容
            size_t newCapacity = 2 * _capacity;
            if(_size + len > newCapacity)
            {
                newCapacity = _size + len;
            }
            reserve(newCapacity);
        }
        //挪动数据
        size_t i = _size + 1 + len;
        while(i > pos)
        {
            *(_str + i) = *(_str + i - len);
            i--;
        }
        size_t index = pos;
        //赋值
        for(size_t i = 0;i < len;i++)
        {
            *(_str + index++) = *(str + i);
        }
        //更新_size
        _size += len;
    }
    //删除pos位置开始的len个字符
    void string::erase(size_t pos,size_t len)
    {
        assert(pos < _size);
        if(_size - pos <= len)
        {
            len = _size - pos;
            *(_str + pos) = '\0';
        }
        else
        {
            size_t i = pos;
            while(i <= _size - len)
            {
                *(_str + i) = *(_str + i + len);
                i++;
            }
        }
        _size -= len;
    }
    //[]
    char& string::operator[](size_t n)
    {
        assert(n < _size);
        return *(_str + n);
    }
    const char& string::operator[](size_t n) const
    {
        assert(n < _size);
        return *(_str + n);
    }
    //取尾数据
    char& string::back()
    {
        return *(_str + _size -1);
    }
    const char& string::back() const
    {
        return *(_str + _size -1);
    }
    //取头数据
    char& string::front()
    {
        return *(_str);
    }
    const char& string::front() const
    {
        return *(_str);
    }
    //交换
    void string::swap(string& s)
    {
        std::swap(_str,s._str);
        std::swap(_size,s._size);
        std::swap(_capacity,s._capacity);
    }
    //+=一个字符
    string& string::operator+=(char ch)
    {
        push_back(ch);
        return *this;
    }
    //+=一个字符串
    string& string::operator+=(const char* str)
    {
        append(str);
        return *this;
    }
    //取子串
    string string::substr(size_t pos,size_t len)
    {
        assert(pos < _size);
        string tmp;
        if(len >= _size - pos)
        {
            len = _size - pos;
        }
        for(size_t i = pos;i < pos + len;i++)
        {
            tmp += *(_str + i);
        }
        return tmp;
    }
    //从pos位置开始查找字符
    size_t string::find(char ch,size_t pos)
    {
        assert(pos < _size);
        for(size_t i = pos;i < _size;i++)
        {
            if(_str[i] == ch)
                return i;
        }
        return npos;
    }
    //从pos位置开始查找字符串
    size_t string::find(const char* str,size_t pos)
    {
        assert(pos < _size);
        const char* ptr = strstr(_str + pos,str);
        if(ptr == nullptr)
        {
            return npos;
        }
        else
        {
            return ptr - _str;
        }
    }
    //流插入
    ostream& operator<<(ostream& out,const string& s)
    {
        //这种方法也可以,但是不建议，在极端情况下会出问题
        //out << s.c_str();
        for(size_t i = 0;i < s.size();i++)
        {
            out << s[i];
        }
        return out;
    }
    //流提取
    istream& operator>>(istream& in,string& s)
    {
        s.clean();
        char buffer[256];
        int index = 0;
        char ch = '\0';
        cin.get(ch);
        while(ch != ' ' && ch != '\n')
        {
            buffer[index++] = ch;
            if(index == 255)
            {
                buffer[index] = '\0';
                s += buffer;
                index = 0;
            }
            cin.get(ch);
        }
        if(index > 0)
        {
            buffer[index] = '\0';
            s += buffer;
        }
        return cin;
    }
    //getline
    istream& getline(istream& in,string& s,char delim)
    {
        s.clean();
        char buffer[256];
        int index = 0;
        char ch = '\0';
        cin.get(ch);
        while(ch != delim)
        {
            buffer[index++] = ch;
            if(index == 255)
            {
                buffer[index] = '\0';
                s += buffer;
                index = 0;
            }
            cin.get(ch);
        }
        if(index > 0)
        {
            buffer[index] = '\0';
            s += buffer;
        }
        return cin;
    }
    //字符串比较大小
    int strcmp(const char* str1,const char* str2)
    {
        assert(str1 && str2);
        while (*str1 == *str2)
        {
            if (*str1 == '\0')
                return 0;
            str1++;
            str2++;
        }
        if (*str1 > *str2)
            return 1;
        else
            return -1;
    }
    //比较大小
    bool operator==(const string& lhs,const string& rhs)
    {
        return !strcmp(lhs.begin(),rhs.begin());
    }
    bool operator!=(const string& lhs,const string& rhs)
    {
        return !(lhs == rhs);
    }
    bool operator>(const string& lhs,const string& rhs)
    {
        return strcmp(lhs.begin(),rhs.begin()) == 1;
    }
    bool operator<(const string& lhs,const string& rhs)
    {
        return !(lhs > rhs || lhs == rhs);
    }
    bool operator>=(const string& lhs,const string& rhs)
    {
        return lhs > rhs || lhs == rhs;
    }
    bool operator<=(const string& lhs,const string& rhs)
    {
        return !(lhs > rhs);
    }
}